#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef double db;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); }
	while(ch>='0'&&ch<='9') { x=(x<<1)+(x<<3)+ch-'0'; ch=getchar(); }
	return x*f;
}
const int MAXN=5e4+10;
int n,m,a[MAXN],f1[17][MAXN],f2[17][MAXN],_mi[17],_log[MAXN];
void build()
{
	for(int i=1;i<=n;++i)f1[0][i]=f2[0][i]=a[i];
	for(int i=1;i<=_log[n];++i)
		for(int j=1;j+_mi[i]-1<=n;++j)
		{
			f1[i][j]=max(f1[i-1][j],f1[i-1][j+_mi[i-1]]);
			f2[i][j]=min(f2[i-1][j],f2[i-1][j+_mi[i-1]]);
		}
	return;
}
int query_max(int l,int r)
{
	int len=_log[r-l+1];
	return max(f1[len][l],f1[len][r-_mi[len]+1]);
}
int query_min(int l,int r)
{
	int len=_log[r-l+1];
	return min(f2[len][l],f2[len][r-_mi[len]+1]);
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;++i)a[i]=read();
	_mi[0]=1;
	for(int i=1;i<=16;++i)
		_mi[i]=_mi[i-1]<<1;
	for(int i=1,j=1,k=-1;i<=n;++i)
		if(i==j)_log[i]=++k,j<<=1;
		else _log[i]=k;
	build();
	while(m--)
	{
		int l=read(),r=read();
		printf("%d\n",query_max(l,r)-query_min(l,r));
	}
	return 0;
}
